/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 *
 * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
 *
 * Contributor(s): 
 *  Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
 *  
 * If you didn't download this code from the following link, you should check
 * if you aren't using an obsolete version: http://www.isqlviewer.com
 */
package org.isqlviewer.sql;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.sql.Clob;
import java.sql.SQLException;

import org.isqlviewer.util.BasicUtilities;

/**
 * Utility Implementation for dealing with CLOB objects in memory.
 * <p>
 * This is very easy implementation of a CLOB using the StringBuffer. This class is designed for easily creating CLOB
 * objects at runtime using Strings.
 * 
 * @see java.lang.StringBuffer
 * @author Markus A. Kobold &lt;mkobold at sprintpcs dot com&gt;
 */
public class ByteArrayClob implements Clob {

    protected StringWriter clobStorage = new StringWriter();
    protected StringBuffer buff = new StringBuffer("");

    public ByteArrayClob(String clobData) throws IOException {

        this(clobData.toCharArray());
    }

    public ByteArrayClob(char[] clobData) throws IOException {

        clobStorage.write(clobData);
        buff = clobStorage.getBuffer();
    }

    public ByteArrayClob(Reader reader) throws IOException {

        BasicUtilities.copyReader(reader, clobStorage);
        buff = clobStorage.getBuffer();
    }

    public long length() {

        return buff.length();
    }

    public String getSubString(long pos, int length) {

        return buff.substring((int) pos, (int) pos + length);
    }

    public Reader getCharacterStream() {

        return new InputStreamReader(getAsciiStream());
    }

    public InputStream getAsciiStream() {

        String buffer = buff.toString();
        return new ByteArrayInputStream(buffer.getBytes());
    }

    public long position(String searchstr, long start) {

        String x = clobStorage.toString();
        return x.indexOf(searchstr, (int) start);
    }

    public long position(Clob searchstr, long start) {

        return 0;
    }

    public int setString(long pos, String str) throws SQLException {

        return setString(pos, str, 0, str.length());
    }

    public int setString(long pos, String str, int offset, int len) throws SQLException {

        buff.insert((int) pos, str.toCharArray(), offset, len);
        return len;
    }

    public OutputStream setAsciiStream(long pos) {

        return new ByteArrayOutputStream();
    }

    public Writer setCharacterStream(long pos) {

        return new StringWriter();
    }

    public void truncate(long len) throws SQLException {

        buff.setLength((int) len);
    }
}